package com.sctvcloud.linshui.ui.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.util.SparseArray;

import com.sctvcloud.linshui.beans.ShortVideoTypeBean;
import com.sctvcloud.linshui.ui.fragment.ShortVideoFragment;

import java.lang.ref.WeakReference;
import java.util.List;

/**
 * 短视频中分类显示
 */
public class ShortVideoPagerAdapter extends FragmentStatePagerAdapter {
    private List<ShortVideoTypeBean> shortVideoTypeList;
    private SparseArray<WeakReference<ShortVideoFragment>> fragmentWeaks;

    public ShortVideoPagerAdapter(FragmentManager fm, List<ShortVideoTypeBean> list) {
        super(fm);
        shortVideoTypeList = list;
        fragmentWeaks = new SparseArray<>();
    }


    public void setShortVideoTypeList(List<ShortVideoTypeBean> shortVideoTypeList) {
        this.shortVideoTypeList = shortVideoTypeList;
        notifyDataSetChanged();
    }

    @Override
    public Fragment getItem(int position) {
        ShortVideoFragment fragment = new ShortVideoFragment();
        fragment.setVideoNoteId(shortVideoTypeList.get(position).getChannelId());
        fragmentWeaks.put(position, new WeakReference<ShortVideoFragment>(fragment));
        return fragment;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return shortVideoTypeList.get(position).getChannelName();
    }

    @Override
    public int getCount() {
        return shortVideoTypeList.size();
    }

}